(function( global,$) {
	var ids = 0; //公共序号
	
	var _args = function(args){
		var title,content,time,hideCallback;
		if(args.length === 1){
			content = args[0];
		}else if(args.length === 2){
			if($.isFunction(args[1])){content = args[0];hideCallback = args[1];
			}else if($.isNumeric(args[1])){content = args[0];time = args[1];
			}else{title = args[0];content = args[1];}
		}else if(args.length === 3){
			if($.isFunction(args[2])){hideCallback = args[2];
				if($.isNumeric(args[1])){content = args[0];time = args[1]
				}else{title = args[0];content = args[1];}
			}else if($.isNumeric(args[2])){
				title = args[0];content = args[1];time = args[2];
			}else{title = args[0];content = args[1];}
		}else{title = args[0];content = args[1];time = args[2];hideCallback = args[3];}
		return {title:title,content:content,time:time,hideCallback:hideCallback}
	}
	
	global.jForest = {
		getId:function(){return (ids++);},
		nextPage:function(pageNo,pageLimit,dataTotal){
			return dataTotal > (pageNo * pageLimit);
		},
		getAjax:function(options){return new Ajax(options);},
		request:function(url){
			if(url){
				global.document.location.href=url;
			}else{
				url = global.document.location.href.replace(/#r=.*/,'');
				global.document.location.href = url+'#r='+Math.random();
			}
		},
		format1:function(str){
			if(str){return str.replace(/"/g,'');}
			return str;
		},
		isMobile:function(){
			return navigator.userAgent.match(/(iPhone|iPod|Android|ios)/i);
		},
		niceCheckbox:function(fn){
			$('.nice-checkbox input[type="checkbox"]').each(function(i,el){
				if(el.checked){$(el).parent().addClass('on');}
			});
			$('.nice-checkbox').mousedown(function(event){
				$(this).toggleClass('on');
				var el = $(this).find('input[type="checkbox"]')[0];
				el.checked = (el.checked == false);
				if(fn){
					var result = fn.call(this,el);
					if(result != null && el.checked != result){
						$(this).toggleClass('on');
						el.checked = result;
					}
				}
			});
		},
		prompt:function(options){
			return new Prompt(options);
		},
		/*参数：title，content，time，hideCallback*/
		error:function(){
			var args = _args(arguments);
			jf.prompt({className:'alert-danger',
				title:args.title,
				content:args.content,
				intervalTime:args.time,
				hideCallback:args.hideCallback
			}).show();
		},
		/*参数：title，content，time，hideCallback*/
		info:function(){
			var args = _args(arguments);
			jf.prompt({className:'alert-info',
				title:args.title,
				content:args.content,
				intervalTime:args.time,
				hideCallback:args.hideCallback
			}).show();
		},
		warning:function(){
			var args = _args(arguments);
			jf.prompt({className:'alert-warning',
				title:args.title,
				content:args.content,
				intervalTime:args.time,
				hideCallback:args.hideCallback
			}).show();
		},
		alert:function(title,msg,click){
			jf.alertFlag=false,click=$.isFunction(click)?click:function(flag){};
			if(!jf.alertModal){jf.alertModal=$('<div class="modal fade" tabindex="-1"><div class="modal-dialog">'
				+'<div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button>'
				+'<h5 class="modal-title">'+title+'</h5></div><div class="modal-body">'+msg+'</div>'
				+'<div class="modal-footer"><button type="button" class="btn btn-default btn-sm" data-dismiss="modal">取消</button>'
				+'<button type="button" class="btn btn-primary btn-sm">确定</button></div></div></div></div>');
			jf.alertModal.find('.btn-primary').click(function(){jf.alertFlag=true,jf.alertModal.modal('hide');});}
			else{jf.alertModal.find('.modal-title').html(title);jf.alertModal.find('.modal-body').html(msg);}
			jf.alertModal.one('hidden.bs.modal',function(){click.call(this,jf.alertFlag)});jf.alertModal.modal('show');
		}
	};

	var Prompt = function(options){
		this.options = $.extend({
			title:'系统提示',
			content:'',
			className:'alert-danger',
			zIndex:1060,
			intervalTime:null,
			hideCallback:function(){}
		},options)
		var my = this,opts = this.options;
		this.$el = $('<div class="modal fade in" style="display:block;padding-top:70px;z-index:'+opts.zIndex+';"></div>')
			.click(function(){my.hide.apply(my)})
			.append('<div class="alert '+opts.className+'" style="max-width:30%;margin:3px auto;padding:10px;"><strong>'
					+opts.title+'</strong>：'+opts.content+'</div>');
	}

	Prompt.prototype = {
		show:function(){
			var my=this,opts=this.options;
			this.$el.appendTo('body');
			if(opts.intervalTime){
				this.timer = setInterval(function(){
					my.hide.apply(my);
				},opts.intervalTime);
			}
		},
		hide:function(){
			if(this.timer){
				clearInterval(this.timer);
				delete this.timer;
			}
			this.$el.remove();
			if($.isFunction(this.options.hideCallback)){
				this.options.hideCallback.apply();
			}
		}
	}
	
	var Ajax = function(options){
		this.options = $.extend({
			bindObj:null,	//绑定的对象
			method:'post',
			dataType:'json',
			url:'',
			param:null,  //方法或对象
			callback:function(){}
		},options);
		this.loadedParam = {}; //上一次加载时用的参数
	}
	
	Ajax.prototype = {
		getParam:function(param){
			var opts=this.options;
			if($.isFunction(opts.param)){
				return opts.param.apply(opts.bindObj||this,[this.loadedParam,param]);
			}else if(param){ return param;
			}else{return this.loadedParam;}
		},
		getCallback:function(callback){
			if($.isFunction(callback)){return callback;}
			else if($.isFunction(this.options.callback)){return this.options.callback;}
			else{return function(result){};}
		},
		isValid:function(){return this.options.url?true:false;},
		load:function(param,callback){
			if(!callback && $.isFunction(param)){callback=param;param=null;}
			var obj = this.options.bindObj||{},opts=this.options;
			this.loadedParam = this.getParam(param);callback = this.getCallback(callback);
			$.ajax({url:opts.url,data:this.loadedParam,dataType:opts.dataType,type:opts.method,
				success:function(result){callback.apply(obj,[result||{}]);}})
		}
	}
	
	global.jf = global.jForest;
})(typeof window !== "undefined" ? window : this,jQuery);

/* 表单提交－$(...).ajaxSubmit() */
(function($){
	$.fn.ajaxSubmit = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			var url = $this.attr('action');
			var method = $this.attr('method') || 'POST';
			var callback = function(){};
			var opts;
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
			}else if(args.length === 1){
				if (typeof(args[0]) === "string"){
					url = args[0];
				}else{
					callback = args[0];
				}
			}else if(args.length === 2){
				url = args[0];
				callback = args[1];
			}
			
			opts = $.extend({
				url:url,
				data:$this.serializeArray(),
				async:true,
				type: method,
				dataType:'json',
				success:callback
			},opts);
			
			$.ajax(opts);
	    });
	    return (value === undefined) ? this : value;
	};
})(window.jQuery);

